package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.vo.countByTimeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 预约信息Service业务层处理
 *
 * @author hubuyi
 * @date 2025-02-27
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id) {
        return getById(id);
    }

    /**
     * 查询预约信息列表
     *
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation) {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */

    /**
     * 修改预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation) {
        return updateById(reservation) ? 1 : 0;
    }

    /**
     * 批量删除预约信息
     *
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除预约信息信息
     *
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 获取今日取消预约人数
     *
     * @return
     */
    @Override
    public int getTodayCancelReservation() {
        //-- 查询当天取消预约数量 select * from reservation where status = 2
        //               and update_by = 2 and update_time between '2022-03-01' and '2022-03-31';
        Long count = this.lambdaQuery().eq(Reservation::getStatus, 2)
                .eq(Reservation::getUpdateBy, UserThreadLocal.getUserId())
                .ge(Reservation::getUpdateTime, DateUtils.getDate())
                .le(Reservation::getUpdateTime, DateUtils.getDate())
                .count();
        return count.intValue();


    }

    /**
     * 根据时间查询预约人数
     *
     * @param time
     * @return
     */
    @Override
    public List<countByTimeVo> getCountByTime(long time) {

//        LocalDateTime ldt = LocalDateTimeUtil.of(time);
//        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
//        LocalDateTime endTime = startTime.plusDays(1);

        LocalDateTime startTime = LocalDateTimeUtil.of(time);
        LocalDateTime endTime = startTime.plusDays(1).toLocalDate().atStartOfDay();
        List<countByTimeVo> countByTime = reservationMapper.getCountByTime(startTime, endTime);
        return countByTime;






    }

    /**
     * 新增预约信息
     *
     * @param reservation
     * @return
     */
    @Override
    public int insertReservation(Reservation reservation) {
        //判断状态,0：待报道，1：已完成，2：取消，3：过期
        reservation.setStatus(0);
        reservation.setCreateBy(String.valueOf(UserThreadLocal.getUserId()));
        reservation.setUpdateBy(String.valueOf(UserThreadLocal.getUserId()));
//        int i = reservationMapper.insertReservation(reservation);
        int insert = reservationMapper.insert(reservation);
        return insert == 1 ? 0 : 1;
    }
}
